package com.ruoyi.rzgl.rzsq.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.rzgl.rzsq.domain.RzglRzsq;
import com.ruoyi.rzgl.rzsq.domain.RzglRzsqFj;
import com.ruoyi.rzgl.rzsq.service.IRzglRzsqService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 软著申请Controller
 *
 * @author huacai
 * @date 2024-10-20
 */
@RestController
@RequestMapping("/rzgl/rzsq")
public class RzglRzsqController extends BaseController {
    @Resource
    private IRzglRzsqService rzglRzsqService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询软著申请列表
     */
    @PreAuthorize("@ss.hasPermi('rzgl:rzsq:list')")
    @GetMapping("/list")
    public TableDataInfo list(RzglRzsq rzglRzsq) {
        startPage();
        List<RzglRzsq> list = rzglRzsqService.selectRzglRzsqList(rzglRzsq);
        return getDataTable(list);
    }

    /**
     * 导出软著申请列表
     */
    @PreAuthorize("@ss.hasPermi('rzgl:rzsq:export')")
    @Log(title = "软著申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RzglRzsq rzglRzsq) {
        List<RzglRzsq> list = rzglRzsqService.selectRzglRzsqList(rzglRzsq);
        ExcelUtil<RzglRzsq> util = new ExcelUtil<RzglRzsq>(RzglRzsq.class);
        util.exportExcel(response, list, "软著申请数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RzglRzsq> util = new ExcelUtil<RzglRzsq>(RzglRzsq.class);
        util.importTemplateExcel(response, "软著申请数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "软著申请", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('rzgl:rzsq:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RzglRzsq> util = new ExcelUtil<RzglRzsq>(RzglRzsq.class);
        InputStream inputStream = file.getInputStream();
        List<RzglRzsq> list = util.importExcel(inputStream);
        inputStream.close();
        int count = rzglRzsqService.batchInsertRzglRzsq(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取软著申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('rzgl:rzsq:query')")
    @GetMapping(value = "/{rzsqId}")
    public AjaxResult getInfo(@PathVariable("rzsqId") String rzsqId) {
        return success(rzglRzsqService.selectRzglRzsqByRzsqId(rzsqId));
    }

    /**
     * 新增软著申请
     */
    @PreAuthorize("@ss.hasPermi('rzgl:rzsq:add')")
    @Log(title = "软著申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RzglRzsq rzglRzsq) {
        rzglRzsq.setUserId(Math.toIntExact(getUserId())); //用户ID
        rzglRzsq.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        rzglRzsq.setCreateBy(getUsername()); //创建人
        int i = rzglRzsqService.insertRzglRzsq(rzglRzsq);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(rzglRzsq.getRzsqId());
        infoRunTaskData.setBusinessType(11); //业务模块: 软著管理-软著申请
        infoRunTaskData.setBusinessTitle(rzglRzsq.getRjName()); //任务名称: 软件名称
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);
        return toAjax(i);
    }

    /**
     * 修改软著申请
     */
    @PreAuthorize("@ss.hasPermi('rzgl:rzsq:edit')")
    @Log(title = "软著申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RzglRzsq rzglRzsq) {
        rzglRzsq.setUpdateBy(getUsername()); //修改人
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(rzglRzsq.getRjName()); //任务名称: 软件名称
        infoRunTaskData.setBusinessKey(rzglRzsq.getRzsqId());
        infoRunTaskData.setBusinessType(11); //业务模块: 软著管理-软著申请
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(rzglRzsqService.updateRzglRzsq(rzglRzsq));
    }

    /**
     * 删除软著申请
     */
    @PreAuthorize("@ss.hasPermi('rzgl:rzsq:remove')")
    @Log(title = "软著申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rzsqIds}")
    public AjaxResult remove(@PathVariable String[] rzsqIds) {
        //同时删除业务关系数据
        for (String rzsqId : rzsqIds) {
            RzglRzsq rzglRzsq = rzglRzsqService.selectRzglRzsqByRzsqId(rzsqId);
            iInfoRunTaskDataService.deleteData(rzglRzsq.getRzsqId(), 11);//业务模块: 软著管理-软著申请
        }
        return toAjax(rzglRzsqService.deleteRzglRzsqByRzsqIds(rzsqIds));
    }

    /**
     * 获得软著申请主键ID (UUID)
     */
    @GetMapping("/getRzSqId")
    public AjaxResult getRzSqId() {
        String getRzSqId = IdUtils.fastSimpleUUID();
        return success(getRzSqId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertRzsqFjByRzsqId(@RequestBody RzglRzsqFj rzglRzsqFj) {
        return toAjax(rzglRzsqService.insertRzsqFjByRzsqId(rzglRzsqFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectRzsqFjList(String sId) {
        RzglRzsqFj rzglRzsqFj = new RzglRzsqFj();
        rzglRzsqFj.setRzsqId(sId);
        return AjaxResult.success(rzglRzsqService.selectRzsqFjList(rzglRzsqFj));
    }

    /**
     * 删除附件资料
     */
    @Log(title = "附件资料", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteRzglRzsqFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(rzglRzsqService.deleteRzglRzsqFjByFjIds(fjIds));
    }

}
